;Copyright (C) 2010 Salil Bhagurkar

;This file is part of k3

;k3 is free software: you can redistribute it and/or modify
;it under the terms of the GNU Lesser General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.

;k3 is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU Lesser General Public License for more details.

;You should have received a copy of the GNU Lesser General Public License
;along with k3.  If not, see <http://www.gnu.org/licenses/>.


;Startup file for the whole kernel
; Named as 1.s so that the stupid build system that was used earlier, or which may still be used
; understands that this file should be linked first to get the grub multiboot header first in the
; kernel binary

[BITS 32]
[segment .text]
SYS_CODE_SEL equ 0x8
SYS_DATA_SEL equ 0x10
global start
start:
cli
	mov esp,0x90000
	jmp stublet


;This is the multiboot header required by grub loader. it specifies the code,data size

ALIGN 4
mboot:

    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end


    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM


    dd mboot
    dd code
    dd bss
    dd end
    dd start

stublet:

    nop
    nop
    nop

    mov [_multiboot_info], ebx

extern _main
extern __setgdt
extern __setidt

call __setgdt
call __setidt

call _main

jmp $


global _callbios
_callbios:
pusha
call SYS_CODE_SEL:0x600
popa
ret

global _get_stack_address
_get_stack_address:
mov eax, esp
ret

extern _arch_null_execution_trap
;Used to trap null pointer execution. This code is moved to 0x0
null_trap_start db "null_trap_start"
global _null_trap
_null_trap dd _null_trap_code
_null_trap_code:
call 0x08:_arch_null_execution_trap
null_trap_end db "null_trap_end"


section .data

global _multiboot_info
_multiboot_info dd 0

global _arch_kernel_start
_arch_kernel_start dd start
global _arch_kernel_end
_arch_kernel_end dd end
